﻿Public Class ServiceLocator
    Private Shared mServices As Dictionary(Of Type, Object)
    Shared Sub New()
        mServices = New Dictionary(Of Type, Object)
    End Sub

    Shared Sub RegisterService(Of T)(implementation As T)
        mServices.Add(GetType(T), implementation)
    End Sub

    Shared Function Resolve(Of T)() As T
        Dim implementation As Object = Nothing
        If Not mServices.TryGetValue(GetType(T), implementation) Then
            Throw New InvalidOperationException(String.Format("No implementation of {0} is registered.", GetType(T)))
        End If
        Return CType(implementation, T)
    End Function

    Public Shared Sub Reset()
        mServices.Clear()
    End Sub
End Class
